// import axios from "axios"
import qs from "qs";

// import { createApp } from 'vue';
// import App from './App.vue';
import { createStore } from 'vuex'
import {getPermisson  } from '@/api/api.js'
import { RouterListFormat } from '@/utils/userPermisson'
import { add} from '@/utils/userPermisson'


export default createStore({
    state: {
        user_roleid: '',
    hasAuth: false,
    routerList: []
    },
    mutations: {
        setAuth(state, hasAuth) {
            state.hasAuth = hasAuth
        }
    },
    actions: {
            // async getPermisson({ state, commit }) {
    //     await axios({
    //         method: 'get',
    //         url: '/userenter/menu',
    //         data: qs.stringify({
    //             id: state.user_roleid
    //         })
    //     }).then((res) => {
    //         state.routerList = RouterListFormat(res.data.data.list) //转为tree型
    //     }).catch(err => {
    //         console.log(err.message)
    //     })
    //     commit('setAuth', true)//只有获取到返回值的时候才修改用户权限
    
   
    // }
    async getPermissonData({commit}){
        let result=await getPermisson()
        console.log('仓库权限result')
        console.log(result)
        if(result.data.status===200){
            console.log('state数据赋值')
            // console.log(result.data.data.list)
            function RouterListFormat(data) {
                let parents = data.filter(p => p.menu_parent_id === 0),
                    children = data.filter(c => c.menu_parent_id != 0)
                dataTotree(parents, children)
                //递归函数
                function dataTotree(parents, children) {
                    parents.map((p) => {
                        children.map((c, index) => {
                            if (c.menu_parent_id === p.menu_parent_id) {//如果相同，拷贝children一个新的数组c1
                                let c1 = Object.assign([], children)
                                c1.splice(index, 1)//删除当前元素，获取除当前元素之外的所有子路由元素
                                dataTotree([c], c1);//继续递归
                                if (p.children) {//如果该结点已经存在孩子，则直接加入孩子
                                    p.children.push(c)
                                } else {//如果该结点不存在孩子，则让该结点的孩子等于数组c
                                    p.children = [c]
                                }
                            }
                        })
                    })
                }
                return parents
            }
      
           this.state.routerList = RouterListFormat(result.data.data.list) //转为tree型
            console.log('routerList---父级菜单')
            console.log( this.state.routerList)
        }
        // commit('setAuth', true)//只有获取到返回值的时候才修改用户权限
     }
    },
    modules: {
    }
  })